package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.controller.form.NewVoteForm;
import com.woniuxy.controller.form.PageAllVoteForm;
import com.woniuxy.controller.info.PageAllVoteInfo;
import com.woniuxy.dao.VoteTopicDao;
import com.woniuxy.model.VoteOption;
import com.woniuxy.model.VoteTopic;
import com.woniuxy.service.VoteOptionService;
import com.woniuxy.service.VoteTopicService;
import com.woniuxy.util.Result;
import com.woniuxy.util.WoniuxyBeanCopyUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhouzhou
 * @since 2022-06-23
 */
@Service
public class VoteTopicServiceImpl extends ServiceImpl<VoteTopicDao, VoteTopic> implements VoteTopicService {

    @Resource
    private RedisTemplate<String,Object> ort;

    @Resource
    private VoteTopicService voteTopicService;

    @Resource
    private VoteOptionService voteOptionService;

    @Override
    public Result createVote(NewVoteForm newVoteForm) throws Exception {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //添加vote_topic表
        VoteTopic newTopic = new VoteTopic(null,newVoteForm.getTopicName(),
                1, LocalDateTime.now() ,LocalDateTime.parse(newVoteForm.getCloseDate(),df));
        voteTopicService.save(newTopic);
        ort.opsForHash().put("topic:"+newTopic.getTopicId(),"topic_title",newTopic.getTopicTitle());
        ort.opsForHash().put("topic:"+newTopic.getTopicId(),"topic_state",newTopic.getTopicState()+"");
        ort.opsForHash().put("topic:"+newTopic.getTopicId(),"topic_start_date",newTopic.getTopicStartDate().toString());
        ort.opsForHash().put("topic:"+newTopic.getTopicId(),"topic_end_date",newTopic.getTopicEndDate().toString());
        Duration duration = Duration.between(newTopic.getTopicStartDate(),newTopic.getTopicEndDate());
        ort.expire("topic:"+newTopic.getTopicId(),duration.toMillis(), TimeUnit.MILLISECONDS);
        //写进选项表
        System.out.println(newVoteForm);
        for (String eachOption : newVoteForm.getOption()){
            System.out.println(eachOption);
            VoteOption newVoteOption = new VoteOption(eachOption,newTopic.getTopicId());
            voteOptionService.save(newVoteOption);
        }

        return Result.success(200,"添加成功",null);
    }

    @Override
    public Result getAllVote(PageAllVoteForm pageAllVoteForm) {
        ObjectMapper mapper = new ObjectMapper();
        PageAllVoteForm page = voteTopicService.page(pageAllVoteForm,null);
        PageAllVoteInfo pageAllVoteInfo = WoniuxyBeanCopyUtil.copyOne(pageAllVoteForm, PageAllVoteInfo.class);
        System.out.println(pageAllVoteInfo.toString());
        return Result.success(200,"查询全部主题成功",pageAllVoteInfo.toString());
    }
}
